package com.example.cdpbookstore.dao.impl;

import com.example.cdpbookstore.dao.OrderDao;
import com.example.cdpbookstore.pojo.entity.Order;
import com.example.cdpbookstore.tool.C3P0Tool;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public boolean save(Order order) {
        String sql = "insert into tb_order (id, customerId, creationTime, updateTime) values (?, ?, ?, ?)";

        try {
            int update = C3P0Tool.queryRunner.update(sql,
                    order.getId(),
                    order.getCustomerId(),
                    order.getCreationTime(),
                    order.getUpdateTime());
            return update > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Order> listByCustomerId(String customerId) {
        String sql = "select * from tb_order where customerId=? and state=1 order by creationTime desc";
        try {
            return C3P0Tool.queryRunner.query(sql, new BeanListHandler<>(Order.class), customerId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean removeById(String id) {
        String sql = "update tb_order set state=0 where id=?";
        try {
            int update = C3P0Tool.queryRunner.update(sql, id);
            return update > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
